package com.dataProcess;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.enetiy.Areainfo;
import com.enetiy.Areanet;
import com.enetiy.Areausable;
import com.enetiy.Binding;
import com.enetiy.Comboinfo;
import com.enetiy.Keeptime;
import com.enetiy.Userinfo;
import com.enetiy.Vipmember;
import com.operation.FreeLink;
import com.service.AreainfoService;
import com.service.AreausbleService;
import com.service.BindingService;
import com.service.ComboService;
import com.service.DealService;
import com.service.UserinfoService;
import com.util.NetTimeUtil;
import com.util.Verify;

public class VerifyResou {
	
	
	public List<Areainfo> areainfos = null;
	
	/**
	 * 
	 * 验证用户绑定资源
	 * @param userid
	 * @param siteAreaid
	 * @param areausbleService
	 * @param bindingService
	 * @return
	 * @throws Exception
	 */
	public int bandingUsable;//登录区域是否有可绑绑定
		public Binding verifyBinding(int userid,int siteAreaid, 
					AreausbleService areausbleService,BindingService bindingService) throws Exception{
			Binding binding = null;
			int band = 0;
			//此区域所有资源
			List<Areausable> areabList = areausbleService.selectAreabList(siteAreaid);
			if(!areabList.isEmpty()){
				for (int i = 0; i < areabList.size(); i++) {
					//此区域可用绑定资源
					String netKeyword  =areabList.get(i).getAreainfo().getAreanet().getKeyword();//连网type
					
					if(netKeyword.contains("NF-net-0")){//绑定type
						bandingUsable=1;//标示 有可绑绑定
						//用户所有绑定
						List<Binding> bindings = bindingService.selsctBindingUsers(userid);
						
						if(bindings!=null&&!bindings.isEmpty()){//如不为空
							for (int j = 0; j < bindings.size(); j++) {//遍历用户所有绑定
								// 用户所拥有的绑定所属绑定资源  中 包含 此区域可用绑定资源
								if(areabList.get(i).getAreainfo().getId()==bindings.get(j).getAreainfo().getId()){
									//并且 速率 大于其他绑定
									if(bindings.get(j).getAreainfo().getAreatype().getBanfwidth()>band)
										//输出其绑定
										binding = bindings.get(j);
										band=binding.getAreainfo().getAreatype().getBanfwidth();
								}
							}
						}
					}
				}
			}
			return binding;//可用绑定
		}
		
		
	
		/**
		 * 验证用户免费资源
		 * @param userid
		 * @param siteAreaid
		 * @param userinfoService
		 * @param areainfoService
		 * @return 
		 * @throws Exception
		 */
		public int freeUsable;//免费可用
		public String freekeeptime;//
		public int freeIden;
		public int freeTime;
		
		public Areainfo verifyFree(int userid,int siteAreaid, UserinfoService userinfoService,
				AreainfoService areainfoService,DealService dealService,String gateIp) throws Exception{
			
			Areainfo areainfo = null;
			//此区域所有资源
			areainfos = areainfoService.selectAreainfo(siteAreaid);
			
			if(areainfos!=null&&!areainfos.isEmpty()){
				for (int i = 0; i < areainfos.size(); i++) {
					String keywor = areainfos.get(i).getAreanet().getKeyword();
					if(keywor.equals("NF-net-31")){
						int timing = FreeLink.timingFristFreeVerify(userid, areainfos.get(i).getVerifyUrl(), gateIp);
						if(timing==1){
							areainfo = areainfos.get(i);
							freeUsable=1;
							freeIden=areainfos.get(i).getId();
							freeTime = areainfos.get(i).getValidTime();
						}else{
							//查询计时 , 免费上网计时type为2
							Keeptime keeptime = dealService.selectKeeptime(userid, 2,1, siteAreaid);
							if(keeptime!=null && NetTimeUtil.getPareTimeUtil(keeptime.getKeepTime(),"yyyy-MM-dd HH:mm:ss")){
								freekeeptime = keeptime.getKeepTime();
								areainfo = areainfos.get(i);
							}
						}
					//....
					}else if(keywor.equals("NF-net-30")){
						
					}
				}
			}
			return areainfo;
		}
		
		
		/**
		 * 验证被授权资源
		 * @param userid
		 * @param siteAreaid
		 * @param userinfoService
		 * @param areainfoService
		 * @return 
		 * @throws Exception
		 */
		public Areainfo verifyQR(int siteAreaid,AreainfoService areainfoService) throws Exception{
			
			Areainfo areainfo = null;
			//此区域所有资源
			areainfos = areainfoService.selectAreainfo(siteAreaid);
			
			if(areainfos!=null&&!areainfos.isEmpty()){
				for (int i = 0; i < areainfos.size(); i++) {
					if(areainfos.get(i).getAreanet().getKeyword().equals("NF-net-40")){
							areainfo = areainfos.get(i);
					}
					//....
				}
			}
			return areainfo;
		}
		
		/**
		 * 验证套餐资源
		 * @param userid
		 * @param siteAreaid
		 * @param comboService
		 * @param areainfoService
		 * @return
		 * @throws Exception
		 */
		//区域套餐
		public int comboUsable;//登录区域是否有可购买套餐
		public List<Integer> verifyAreaCombo(int userid,int siteAreaid) throws Exception{
			List<Integer> comboWay= new ArrayList<Integer>();
			//区域可用资源模式
			if(areainfos != null && !areainfos.isEmpty()){
				for (int i = 0; i < areainfos.size(); i++) {
					Areanet areanet = areainfos.get(i).getAreanet();
					if(areanet.getKeyword().contains("NF-net-1")){
						comboUsable = 1;//标示 可购买套餐
						comboWay.add(areanet.getId());//添加
					}
				}
			}
			return comboWay;
		}
		//用户套餐
		public List<Comboinfo> userComboList;//
		public Comboinfo verifyUserCombo(ComboService comboService,int userid) throws Exception{
			Comboinfo comboinfo=null;
			Comboinfo useComboinfo = null;
			//用户所拥有套餐
			userComboList = comboService.selectUserComboList(userid);
			int band = 0;//带宽
			if(userComboList!=null&&!userComboList.isEmpty()){
				for (int j = 0; j < userComboList.size(); j++) {
					Comboinfo pastCombo = Verify.pastCombo(userComboList.get(j), comboService);
					if(pastCombo.getBandwidth()>=band){
						comboinfo=pastCombo;
					}
					if(pastCombo.getStatus()==1){
						useComboinfo =pastCombo;
					}
				}
			}
			if(useComboinfo!=null){
				comboinfo = useComboinfo;
			}
			return comboinfo;
		}
		
	/**
	 * 验证用户会员资源
	 * @param userid
	 * @param siteAreaid
	 * @param dealService
	 * @param areainfoService
	 * @return
	 * @throws Exception 
	 */
		public int vipUsable;
		public Keeptime vipKeeptime;
		public List<Integer> verifyAreaVip(int userid,int siteAreaid) throws Exception{
			List<Integer> vipWay = new ArrayList<Integer>();
			if(areainfos!=null&&!areainfos.isEmpty()){
				for (int s = 0; s < areainfos.size(); s++) {
					Areanet areanet = areainfos.get(s).getAreanet();
					if(areanet.getKeyword().contains("NF-net-2")){
						vipWay.add(areanet.getId());
					}
				}
			}
			return vipWay;
		}
		public List<Vipmember> userVipList;//
		public Vipmember verifyUserVip(int userid,int siteAreaid,DealService dealService,int device) throws Exception{
			Vipmember vipmember=null;
			userVipList = dealService.selectVipmember(userid);
			if(userVipList!=null&&!userVipList.isEmpty()){
				for (int i = 0; i < userVipList.size(); i++) {
					vipUsable=1;
					vipmember=userVipList.get(i);
					if(userVipList.get(i).getWayId()==20){
						break;
					}
				}
			}
			if(vipmember!=null){
				Keeptime keeptime = dealService.selectKeeptime(userid, 5, device,siteAreaid);
				if(keeptime!=null && NetTimeUtil.getPareTimeUtil(keeptime.getKeepTime(),"yyyy-MM-dd HH:mm:ss")){
					vipKeeptime = keeptime;
				}
			}
			return vipmember;
		}
		
		
		/**
		 * 查询模式，和用户资源
		 */
		public Vipmember vipmember;
		public Comboinfo comboinfo;
		public List<Integer>  verifyWay(Userinfo userinfo,ComboService comboService , DealService dealService,int siteAreaid,int device) throws Exception{
			List<Integer> way = new ArrayList<Integer>();
			if(userinfo!=null){
				int userid = userinfo.getUsers().getId();
				if(userinfo.getComboinfo()!=null){
					way.add(userinfo.getComboinfo().getWayId());
					comboinfo =userinfo.getComboinfo();
					comboUsable=1;//标记combo可用
				}else if(userinfo.getVipmember()!=null){
					way.add(userinfo.getVipmember().getWayId());
					vipmember=userinfo.getVipmember();
					Keeptime keeptime = dealService.selectKeeptime(userid, 5,device, siteAreaid);
					if(keeptime!=null &&keeptime.getKeepTime()!=null&& 
							NetTimeUtil.getPareTimeUtil(keeptime.getKeepTime(),"yyyy-MM-dd HH:mm:ss")){
						vipKeeptime = keeptime;
					}
					vipUsable = 1;//标记vip可用
				}else{
					Comboinfo userCombo = verifyUserCombo(comboService, userid);
					if(userCombo!=null){
						way.add(userCombo.getWayId());
						comboinfo =userCombo;
					}else{
						Vipmember userVip = verifyUserVip(userid, siteAreaid, dealService,device);
						if(userVip!=null){
							way.add(userVip.getWayId());
							vipmember=userVip;
						}
					}
				}
				if(way.isEmpty()){
					List<Integer> verifyAreaCombo = verifyAreaCombo(userid, siteAreaid);
					for (int i = 0; i < verifyAreaCombo.size(); i++) {
						way.add(verifyAreaCombo.get(i));
					}
					List<Integer> verifyAreaVip = verifyAreaVip(userid, siteAreaid);
					for (int i = 0; i < verifyAreaVip.size(); i++) {
						way.add(verifyAreaVip.get(i));
					}
				}
				if(comboinfo!=null){
					long time = comboinfo.getEndTime().getTime();
					long time2 = new Date().getTime();
					if(time<time2){
						comboinfo.setStatus(0);
						comboService.updateUserCombo(comboinfo);
					}
				}
			}
			return way;
		}
	}



